/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.

	This file is part of Maxsi Engine.

	Maxsi Engine is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi engine is distributed in the hope that it will be useful, but WITHOUT
	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
	FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
	for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Engine. If not, see <http://www.gnu.org/licenses/>.

	MaxsiEngine.dll
	A general purpose C++ library for the Maxsi Technology Project.

	MaxsiThread.h
	An cross-platform interface for dealing with multithreaded functionality.

******************************************************************************/

#ifndef MaxsiThread_H
#define MaxsiThread_H

BeginMaxsiNamespace

//=============================================================================
//	Forward declarations.
//=============================================================================
class MaxsiThreadSystem;
#ifdef Maxsi_Support_Pthreads
class MaxsiThreadSystemPthreads;
#endif
#ifdef Maxsi_Support_W32
class MaxsiThreadSystemW32;
#endif

#ifdef CreateEvent
#undef CreateEvent
#endif

#ifdef Maxsi_Using_Unicode_Internally
#define CreateEventM CreateEventW
#else
#define CreateEventM CreateEventA
#endif

//=============================================================================
//	Define where the MaxsiThreadSystem interface is declared.
//=============================================================================
#undef MAXSI_INTERFACE
#define MAXSI_INTERFACE MAXSI_INTERFACE_ABSTRACT_BASECLASS

//=============================================================================
//	Define the MaxsiThreadEntry_t function pointer type.
//=============================================================================
typedef Word_t (*MaxsiThreadEntry_t)(BYTE* Parameter);

//=============================================================================
//	MaxsiThreadParameter - A simple structure for holding useful information.
//=============================================================================
STRUCTLINK MaxsiThreadParameter
{
	MaxsiThreadEntry_t	Function;
	BYTE*				FunctionParameter;
};

//=============================================================================
//	MaxsiThreadSystem - An interface to all sorts of thread systems allowing
//	for easy handling of multithreaded environments.
//=============================================================================
CLASSLINK MaxsiThreadSystem
{
public:
	//=============================================================================
	//	Copy-pasteable interface starts here:
	//=============================================================================
	virtual MaxsiHandle CreateThread(MaxsiThreadEntry_t Entry, BYTE* Argument, size_t StackSize = 0) MAXSI_INTERFACE;
	virtual MaxsiHandle ExitThread(int ExitCode) MAXSI_INTERFACE;
	virtual MaxsiHandle GetThreadId() MAXSI_INTERFACE;
	virtual bool CompareThreadIds(MaxsiHandle Handle1, MaxsiHandle Handle2) MAXSI_INTERFACE;
	virtual MaxsiHandle CreateCriticalSection() MAXSI_INTERFACE;
	virtual bool DeleteCriticalSection(MaxsiHandle CriticalSection) MAXSI_INTERFACE;
	virtual bool EnterCriticalSection(MaxsiHandle CriticalSection) MAXSI_INTERFACE;
	virtual bool TryEnterCriticalSection(MaxsiHandle CriticalSection) MAXSI_INTERFACE;
	virtual bool LeaveCriticalSection(MaxsiHandle CriticalSection) MAXSI_INTERFACE;
	virtual MaxsiHandle CreateEvent() MAXSI_INTERFACE;
	virtual bool WaitForEvent(MaxsiHandle Event) MAXSI_INTERFACE;
	virtual bool WaitedForEvent(MaxsiHandle Event) MAXSI_INTERFACE;
	virtual bool SignalEventBroadcast(MaxsiHandle Event) MAXSI_INTERFACE;
	virtual bool SignalEvent(MaxsiHandle Event) MAXSI_INTERFACE;
	virtual bool DeleteEvent(MaxsiHandle Event) MAXSI_INTERFACE;

};

#ifdef Maxsi_Support_Pthreads
//=============================================================================
//	Define where the MaxsiThreadSystemPthreads interface is declared.
//=============================================================================
#undef MAXSI_INTERFACE
#define MAXSI_INTERFACE MAXSI_INTERFACE_IMPLEMENTED_IN_MAXSI_ENGINE

//=============================================================================
//	MaxsiThreadSystemPthreads - An interface to all sorts of thread systems
//	allowing for easy handling of multithreaded environments.
//=============================================================================
CLASSLINK MaxsiThreadSystemPthreads : public MaxsiThreadSystem
{
public:
	virtual MaxsiHandle CreateThread(MaxsiThreadEntry_t Entry, BYTE* Argument, size_t StackSize = 0) MAXSI_INTERFACE;
	virtual MaxsiHandle ExitThread(int ExitCode) MAXSI_INTERFACE;
	virtual MaxsiHandle GetThreadId() MAXSI_INTERFACE;
	virtual bool CompareThreadIds(MaxsiHandle Handle1, MaxsiHandle Handle2) MAXSI_INTERFACE;
	virtual MaxsiHandle CreateCriticalSection() MAXSI_INTERFACE;
	virtual bool DeleteCriticalSection(MaxsiHandle CriticalSection) MAXSI_INTERFACE;
	virtual bool EnterCriticalSection(MaxsiHandle CriticalSection) MAXSI_INTERFACE;
	virtual bool TryEnterCriticalSection(MaxsiHandle CriticalSection) MAXSI_INTERFACE;
	virtual bool LeaveCriticalSection(MaxsiHandle CriticalSection) MAXSI_INTERFACE;
	virtual MaxsiHandle CreateEvent() MAXSI_INTERFACE;
	virtual bool WaitForEvent(MaxsiHandle Event) MAXSI_INTERFACE;
	virtual bool WaitedForEvent(MaxsiHandle Event) MAXSI_INTERFACE;
	virtual bool SignalEventBroadcast(MaxsiHandle Event) MAXSI_INTERFACE;
	virtual bool SignalEvent(MaxsiHandle Event) MAXSI_INTERFACE;
	virtual bool DeleteEvent(MaxsiHandle Event) MAXSI_INTERFACE;

};
#endif

#ifdef Maxsi_Support_W32
//=============================================================================
//	Define where the MaxsiThreadSystemPthreads interface is declared.
//=============================================================================
#undef MAXSI_INTERFACE
#define MAXSI_INTERFACE MAXSI_INTERFACE_IMPLEMENTED_IN_MAXSI_ENGINE

//=============================================================================
//	MaxsiThreadSystemPthreads - An interface to all sorts of thread systems
//	allowing for easy handling of multithreaded environments.
//=============================================================================
CLASSLINK MaxsiThreadSystemW32 : public MaxsiThreadSystem
{
public:
	virtual MaxsiHandle CreateThread(MaxsiThreadEntry_t Entry, BYTE* Argument, size_t StackSize = 0) MAXSI_INTERFACE;
	virtual MaxsiHandle ExitThread(int ExitCode) MAXSI_INTERFACE;
	virtual MaxsiHandle GetThreadId() MAXSI_INTERFACE;
	virtual bool CompareThreadIds(MaxsiHandle Handle1, MaxsiHandle Handle2) MAXSI_INTERFACE;
	virtual MaxsiHandle CreateCriticalSection() MAXSI_INTERFACE;
	virtual bool DeleteCriticalSection(MaxsiHandle CriticalSection) MAXSI_INTERFACE;
	virtual bool EnterCriticalSection(MaxsiHandle CriticalSection) MAXSI_INTERFACE;
	virtual bool TryEnterCriticalSection(MaxsiHandle CriticalSection) MAXSI_INTERFACE;
	virtual bool LeaveCriticalSection(MaxsiHandle CriticalSection) MAXSI_INTERFACE;
	virtual MaxsiHandle CreateEvent() MAXSI_INTERFACE;
	virtual bool WaitForEvent(MaxsiHandle Event) MAXSI_INTERFACE;
	virtual bool WaitedForEvent(MaxsiHandle Event) MAXSI_INTERFACE;
	virtual bool SignalEventBroadcast(MaxsiHandle Event) MAXSI_INTERFACE;
	virtual bool SignalEvent(MaxsiHandle Event) MAXSI_INTERFACE;
	virtual bool DeleteEvent(MaxsiHandle Event) MAXSI_INTERFACE;

};
#endif

//=============================================================================
//	Remember to reset MAXSI_INTERFACE when done with it for safety reasons.
//=============================================================================
#undef MAXSI_INTERFACE
#define MAXSI_INTERFACE MAXSI_INTERFACE_RESET_IT_TO_THIS

//=============================================================================
//	MaxsiThreadSystem* ThreadSystem();
//
//	Returns a pointer to the local thread system that implements the Maxsi
//	Thread System interface on this platform.
//=============================================================================
LINK MaxsiThreadSystem* ThreadSystem();

LINK size_t GetNumberOfCPUs();

EndMaxsiNamespace

#endif

